import { FairyGUI, FairyEditor, System } from 'csharp';
import ListItem from './ListItem';
/**
 * 
 * @author clong 2020.11.01
 */
export default class EGlist extends FairyGUI.EventDispatcher {

    protected list: FairyGUI.GList;
    protected _array: Array<any>;
    protected _thisObject: any;
    protected _itemRenderer: Function = null;

    protected clickItemCallBack: FairyGUI.EventCallback1 = null;
    protected scrollEndCallBack: FairyGUI.EventCallback1 = null;
    /**上一次选择索引 */
    private _preSelectedIndex: number = -1;

    private stageMouseDown:boolean = false;

    constructor(list: FairyGUI.GList, thisObject: any = null) {

        super();

        this.list = list;
        this._thisObject = thisObject;
        this._array = [];
        if (this.list != null) {
            this._thisObject = thisObject || this;
            // this.list.itemRenderer = new Laya.Handler(this, this.listItemRender, null, false);
            // this.list.on(fgui.Events.CLICK_ITEM, this, this.clickItem);
            // this.list.on(fgui.Events.SCROLL_END, this, this.listScrollEnd);
            this.list["$eglist"] = this;

            // console.log( "------>itemRenderer: " + this.list.itemRenderer );

            // System.Delegate.CreateDelegate(  )

            //这个桥怎么用???
            // let itemRenderer = new FairyGUI.ListItemRenderer(this.listItemRender);
            // this.list.itemRenderer = itemRenderer; 

            // this.list.onClickItem = new FairyGUI.EventListener( this.list , )
            this.clickItemCallBack = (context: FairyGUI.EventContext) => {
                this.clickItem(context.sender as FairyGUI.GButton);
            }
            this.list.onClickItem.Add(this.clickItemCallBack);

            this.scrollEndCallBack = (context: FairyGUI.EventContext) => {
                this.listScrollEnd();
            }
            this.list.scrollPane.onScrollEnd.Add( this.scrollEndCallBack );
            // Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.mouseDown);
            // Laya.stage.on(Laya.Event.MOUSE_UP, this, this.mouseUp);
        }
    }

    /**
     * 渲染条目
     */
    protected listItemRender(index: number, obj: FairyGUI.GObject): void {

        if (obj instanceof ListItem) {
            obj.data = this._array && this._array[index];
            obj.setIndex(index);
        }
    }

    private clickItem(target: any): void {

        if (this.isSingle && this._preSelectedIndex != this.list.selectedIndex) {
            this.selectItem(target);//单选tab模式
        } else if (!this.isSingle) {
            this.selectItem(target);
        }
        this._preSelectedIndex = this.list.selectedIndex;
    }

    /**选择条目 */
    private selectItem(item: any): void {

        // this.event(FairyGUI.Events.CLICK_ITEM, item);

        this.DispatchEvent(FairyEditor.FEvents.CLICK_ITEM, item);
    }

    public set array(value: Array<any>) {

        this._array = value || [];
        // this.list.numItems = this._array.length;
    }

    public get array(): Array<any> {

        return this._array;
    }

    /**列表是否为单选模式 */
    public get isSingle(): boolean {

        return this.list.selectionMode == FairyGUI.ListSelectionMode.Single;
    }

    /**
     * 取消选择
     */
    public canelSelect(): void {

        this._preSelectedIndex = -1;
        this.list.ClearSelection();
    }

    private mouseDown(): void {

        this.stageMouseDown = true;
    }

    private mouseUp(): void {

        this.stageMouseDown = false;
    }

    private listScrollEnd(): void {

        this.mouseUp();
    }
}